package web.protocols.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import domain.entities.Centro;
import domain.entities.ProtocoloCompromisopais;
import domain.entities.VendorType;
import domain.entities.Vendorcentral;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.ConcurrencyFailureException;
import services.*;
import services.exceptions.NoProtocoloCompromisopaisSelectedException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: scamisay
 * Date: 11/13/12
 * Time: 3:23 PM
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/Protocols/LocalVendorSelection")
public class LocalVendorSelectionController extends ActionSupport implements ModelDriven {
    private Vendorcentral localVendor;
    private CentralVendorCRUDService centralVendorService;
    private ProtocolCRUDService protocolCRUDService;
    private List<Vendorcentral> list;
    private ProtocolCRUDService protocolService;
    private List<VendorType> vendorTypes;
    private VendorTypeService vendorTypeService;
    private InvestigadorProtocoloCentroService investigadorProtocoloCentroService;
    private ProtocoloCentroService protocoloCentroService;
    private String errorMessage;
    private List<Centro> centros;
    private List<Integer> chosenSites;
    private CentroCRUDService centroCRUDService;

    @Autowired
    public LocalVendorSelectionController(CentralVendorCRUDService centralVendorService, ProtocolCRUDService protocolCRUDService, ProtocolCRUDService protocolService, VendorTypeService vendorTypeService, InvestigadorProtocoloCentroService investigadorProtocoloCentroService, ProtocoloCentroService protocoloCentroService, CentroCRUDService centroCRUDService) {
        this.centralVendorService = centralVendorService;
        this.protocolCRUDService = protocolCRUDService;
        this.protocolService = protocolService;
        this.vendorTypeService = vendorTypeService;
        this.investigadorProtocoloCentroService = investigadorProtocoloCentroService;
        this.protocoloCentroService = protocoloCentroService;
        this.centroCRUDService = centroCRUDService;
        localVendor = new Vendorcentral();
        list = new ArrayList<Vendorcentral>();
        centros = new ArrayList<Centro>();
        vendorTypes =  this.vendorTypeService.findAll();
    }

    @Action(value="main", results={
            @Result(name="success", location="/protocols.localVendorSelection.tiles", type="tiles")
    })
    public String execute() throws NoProtocoloCompromisopaisSelectedException {
        ProtocoloCompromisopais protocoloCompromisopais = protocolCRUDService.recoverProtocoloCompromisopais();
        list = centralVendorService.findAllLocalVendorsByProtocoloCompromisoPais(protocoloCompromisopais);

        centros = protocoloCentroService.getSitesByProtocoloCompromisoPaisAndLocalVendors(
                protocoloCompromisopais,
                new ArrayList<Vendorcentral>());
        if(localVendor.getId()!= null){
            localVendor = centralVendorService.findById(localVendor.getId());
            chosenSites = new ArrayList<Integer>(0);
            for(Centro c : localVendor.getSites()){
                chosenSites.add(c.getId());
            }
        }
        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="main", type="redirectAction"),
            @Result(name="input", location="/protocols.localVendorSelection.tiles", type="tiles")
    })
    public String save() throws NoProtocoloCompromisopaisSelectedException {
        try {
           ProtocoloCompromisopais protocoloCompromisopais = protocolCRUDService.recoverProtocoloCompromisopais();
           List<Centro> misCentros = new ArrayList<Centro>();
           for(Integer id : chosenSites){
              misCentros.add(centroCRUDService.findById(id));
           }
           localVendor.setSites(misCentros);
           centralVendorService.saveLocalVendor(localVendor,protocoloCompromisopais);

        }catch (ConcurrencyFailureException cfe){
            errorMessage = "Someone has just saved the same record you did";
            execute();
            return INPUT;
        }
        return SUCCESS;
    }

    public Vendorcentral getLocalVendor() {
        return localVendor;
    }

    public void setLocalVendor(Vendorcentral localVendor) {
        this.localVendor = localVendor;
    }

    public List<Vendorcentral> getList() {
        return list;
    }

    public void setList(List<Vendorcentral> list) {
        this.list = list;
    }

    public List<VendorType> getVendorTypes() {
        return vendorTypes;
    }

    public void setVendorTypes(List<VendorType> vendorTypes) {
        this.vendorTypes = vendorTypes;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public List<Centro> getCentros() {
        return centros;
    }

    public void setCentros(List<Centro> centros) {
        this.centros = centros;
    }

    public List<Integer> getChosenSites() {
        return chosenSites;
    }

    public void setChosenSites(List<Integer> chosenSites) {
        this.chosenSites = chosenSites;
    }

    @Override
    public Vendorcentral getModel() {
        return localVendor;
    }
}
